<template>
  <div class="login_container">
    <div class="login_box">
      <!-- logo -->
      <div class="herder_box">
        <img src="../assets/login-logo.jpg" alt="" />
      </div>
      <!-- 表单区 -->
      <el-form
        ref="loginReset"
        v-bind:model="loginForm"
        :rules="rules"
        label-width="80px"
        class="login_form"
      >
        <!-- 用户名 -->
        <!-- prop指定验证规则 -->
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="primary" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      //验证用户名和密码  trigger:""状态blue失去焦点
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 10,
            message: '长度在 6 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    //点击重置
    //resetFields方法实现重置loginReset表单
    resetLogin() {
      // console.log(this.$refs.loginReset)
      this.$refs.loginReset.resetFields()
    },
    login() {
      //valid是一个布尔值，成功true
      //validate实现表单的预验证
      this.$refs.loginReset.validate(async (valid) => {
        // console.log('@@',valid)
        if (!valid) rerurn;
        // const reslut = await this.$http.post('login', this.loginForm)
        const { data: res } = await this.$http.post('login', this.loginForm)
        // console.log('qwe',res)
        if (res.meta.status < 200&&res.meta.status > 300) return this.$message.error('登陆失败！')
        this.$message.success('登陆成功！')

        // //将登陆成功之后的token,保存到客户端的sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)

        // //通过编程式导航跳转到后台主页，路由地址是  /home
        this.$router.push('/home')
      })
    },
  },
  mounted() {},
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100vh;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .herder_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #e0e0e0;
    }
  }
}
.btn {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 90%;
}
</style>
